<template>
  <a-modal
    :title='title'
    width='80%'
    :visible='visible'
    :confirmLoading='loading'
    @ok="() => { $emit('ok', timeList) }"
    @cancel="() => { $emit('cancel') }"
  >
    <a-spin :spinning='loading'>
      <a-form :form='form' v-bind='formLayout'>
        <a-row :gutter='24'>
          <a-col :span='12'>
            <a-form-item label='设备编码'>
              <a-input v-model='model.equipmentNo' disabled />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='摄像机开关'>
              <a-switch v-model:checked="model.camera"  checked-children="打开" un-checked-children="关闭" />
            </a-form-item>
          </a-col>
          <a-col :span='12' style='margin-right: 50%'>
            <a-form-item label='充电开关'>
              <a-switch v-model:checked="model.charge"  checked-children="打开" un-checked-children="关闭" />
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='照明开启时间'>
              <a-time-picker  v-model="model.ledOpenTime" format="HH:mm" valueFormat='HH:mm'/>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='照明关闭时间'>
              <a-time-picker  v-model="model.ledCloseTime" format="HH:mm" valueFormat='HH:mm'/>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='满电电压' help='当电压达到满电电压就停止充电,低于断开电压,就会切断负载供电'>
              <a-input v-model="model.fullVoltage" />
              <span style="margin-left:5px">V</span>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='切断供电电压' help='当电压达到满电电压就停止充电,低于断开电压,就会切断负载供电'>
              <a-input v-model="model.shutoffVoltage" />
              <span style="margin-left:5px">V</span>
            </a-form-item>
          </a-col>
          <a-col :span='12' style='margin-right: 50%'>
            <a-form-item label='照明开关'>
              <a-switch v-model:checked="model.led"  checked-children="打开" un-checked-children="关闭" />
            </a-form-item>
          </a-col>
          <a-col :span='12' >
            <a-form-item label='LED照明亮度'>
              <a-input-number
                v-model='model.ledlum'
                :min="0"
                :max="5000"
                :formatter="value => `${value}`"
                :parser="value => value.replace('', '')"
              />
              <span style="margin-left:5px">mA</span>
            </a-form-item>
          </a-col>
          <a-col :span='12'>
            <a-form-item label='路灯亮度'>
              <a-input-number
                v-model='model.lightLitlum'
                :min="0"
                :max="1000"
                :formatter="value => `${value}`"
                :parser="value => value.replace('', '')"
              />
              <span style="margin-left:5px">mA</span>
            </a-form-item>
          </a-col>

          <a-col :span='12' style='margin-right: 50%'>
            <a-form-item label='时段开关'>
              <a-switch v-model:checked="model.periodTimeFlag"  checked-children="打开" un-checked-children="关闭" />
              <a-button type="primary" style='margin-left: 5px' @click="add">
                <a-icon type="plus" /> 新增时段
              </a-button>
            </a-form-item>
          </a-col>
          <div v-for='(item,index) in timeList' :key='index'>
            <a-col :span='8'>
              <a-form-item :label="'时段'+(index+1)">
                <div style='display: flex;align-items: center'>
                  <a-time-picker :disabled='!model.periodTimeFlag' style='margin-right: 5px;' v-model="item.beginPeriodTime" format="HH:mm" valueFormat='HH:mm'/>
<!--                  <span style='margin-right: 5px;'>至</span>-->
<!--                  <a-time-picker :disabled='!model.periodTimeFlag' v-model="item.endPeriodTime" format="HH:mm" valueFormat='HH:mm'/>-->
                </div>
              </a-form-item>
            </a-col>
            <a-col :span='8'>
              <a-form-item label='LED照明亮度'>
                <a-input-number
                  v-model='item.ledlum'
                  :disabled='!model.periodTimeFlag'
                  :min="0"
                  :max="5000"
                  :formatter="value => `${value}`"
                  :parser="value => value.replace('', '')"
                />
                <span style="margin-left:5px">mA</span>
              </a-form-item>
            </a-col>
            <a-col :span='8'>
              <a-form-item label='路灯亮度'>
                <a-input-number
                  v-model='item.lightLitlum'
                  :disabled='!model.periodTimeFlag'
                  :min="0"
                  :max="1000"
                  :formatter="value => `${value}`"
                  :parser="value => value.replace('', '')"
                />
                <span style="margin-left:5px">mA</span>
              <a-icon
                v-if="index > 0"
                class="dynamic-delete-button"
                style='margin-left: 5px'
                type="minus-circle-o"
                :disabled="!model.periodTimeFlag"
                @click="() => remove(item, index)"
              />
              </a-form-item>
            </a-col>
          </div>
          <a-col :span='12'>
            <a-form-item label='上传数据'>
              <a-switch v-model:checked="model.sw"  checked-children="打开" un-checked-children="关闭" />
            </a-form-item>
          </a-col>
          <a-col :span='12' v-if='model.sw'>
            <a-form-item label='间隔时间'>
              <a-input v-model='model.swTime' suffix="s(秒)" />
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>

    </a-spin>
  </a-modal>
</template>

<script>
import pick from 'lodash.pick'
import { getDeviceGroupList } from '@/api/device'
// 表单字段
const fields = ['id', 'alarmsName', 'equipmentNo', 'password', 'alarmsGroup', 'address', 'ambientTemperature', 'chargingCapacity', 'chargingCurrent', 'chargingPower', 'chargingVoltage'
  , 'dischargeCurrent', 'dischargePower', 'dischargeVoltage', 'innerTemperature', 'innerTemperature', 'ledCurrent', 'ledPower', 'ledVoltage', 'streetlightVoltage', 'fullVoltage', 'shutoffVoltage']

export default {
  props: {
    title: {
      type: String,
      required: true
    },
    visible: {
      type: Boolean,
      required: true
    },
    loading: {
      type: Boolean,
      default: () => false
    },
    model: {
      type: Object,
      default: () => null
    }
  },
  watch: {
    model: {
      handler: function (val, oldVal) {
        console.log('model111111', val)
        if (val.periodTimeList) {
          this.timeList = val.periodTimeList
        }
        this.model = val
      },
      deep: true
    }
  },
  data() {

    this.formLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 7 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 13 }
      }
    }
    return {
      form: this.$form.createForm(this),
      groupList: [],
      timeList: [
        {
          // endPeriodTime: '',
          beginPeriodTime: '',
          ledlum: '',
          lightLitlum: '',
        }
      ],
    }
  },
  created() {
    // this.getGroupList()
    console.log('custom modal created')

    // 防止表单未注册
    fields.forEach(v => this.form.getFieldDecorator(v))

    // 当 model 发生改变时，为表单值
    // this.$watch('model', () => {
    //   this.model && this.form.setFieldsValue(pick(this.model, fields))
    //   this.model.alarmsData && this.form.setFieldsValue(pick(this.model.alarmsData, fields))
    // })
  },
  methods: {
    add() {
      this.timeList.push({
        endPeriodTime: '',
        beginPeriodTime: '',
        ledlum: '',
        lightLitlum: '',
      })
    },
    remove(item, index) {
      this.timeList.splice(index, 1)
    },
    getGroupList() {
      var params = {
        pageNum: 1,
        pageSize: 100
      }
      getDeviceGroupList(params).then(res => {
        this.groupList = res.data.list
        console.log('groupList', this.groupList)
      })
    }
  }
}
</script>
 
<style lang='less' scoped>
/deep/ .ant-form-item {
  display: flex;
  align-items: center;
}
/deep/ .ant-input-number{
  width: 150px!important;
}
/deep/ .ant-form-item-children{
  display: flex;
  align-items: center;
}
</style>